﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.IO;
using System.Xml.Linq;
using ProtoType.DataStorage;
using System.Text;
using System.Windows.Markup;
using System.Windows.Navigation;
using System.Collections.ObjectModel;
using ProtoType.ProtoTypeService;

namespace ProtoType
{
    public partial class Design : PhoneApplicationPage
    {
        ProtoTypeServiceClient client;
        AppSettings apps = new AppSettings();
        DataStructureStore dss = new DataStructureStore();
        string selectedfid = "";

        public Design()
        {
            InitializeComponent();
            client = new ProtoTypeServiceClient();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (NavigationContext.QueryString.TryGetValue("fid", out selectedfid))
            {
                tBlockFormId.Text = selectedfid;
                if (apps.IsolatedStorageSavingSetting)
                {
                    btnSelectLayout.Content = dss.GetSelectedLayOutType(selectedfid);
                    btnSelectDetailLayout.Content = dss.GetSelectedDetailLayOutType(selectedfid);
                }
                if (apps.WebServiceSavingSetting)
                {
                    client.GetSelectedLayOutTypeAsync(selectedfid);
                    client.GetSelectedLayOutTypeCompleted += new EventHandler<GetSelectedLayOutTypeCompletedEventArgs>(client_GetSelectedLayOutTypeCompleted);

                    client.GetSelectedDetailLayOutTypeAsync(selectedfid);
                    client.GetSelectedDetailLayOutTypeCompleted += new EventHandler<GetSelectedDetailLayOutTypeCompletedEventArgs>(client_GetSelectedDetailLayOutTypeCompleted);
                }
            }
        }
        void client_GetSelectedLayOutTypeCompleted(object sender, GetSelectedLayOutTypeCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                btnSelectLayout.Content = e.Result.ToString();
            }
        }
        void client_GetSelectedDetailLayOutTypeCompleted(object sender, GetSelectedDetailLayOutTypeCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                btnSelectDetailLayout.Content = e.Result.ToString();
            }
        }

        private void btnlayout_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Layouts.xaml?fid=" + selectedfid, UriKind.Relative));
        }

        private void btnSelectDetailLayout_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/DetailLayoutType.xaml?fid=" + selectedfid, UriKind.Relative));
        }

        private void Image_MouseEnter(object sender, MouseEventArgs e)
        {
            NavigationService.Navigate(new Uri("/ManageControlList.xaml?fid=" + selectedfid, UriKind.Relative));
        }
        //Unneccessary methods removed

        //private void LoadFormDetails(string formid)
        //{
        //    if (apps.IsolatedStorageSavingSetting == true)
        //    {
        //        LoadlayoutControls(formid);
        //    }
        //    if (apps.WebServiceSavingSetting == true)
        //    {
        //        client.LoadlayoutControlsAsync(formid);
        //        client.LoadlayoutControlsCompleted += new EventHandler<LoadlayoutControlsCompletedEventArgs>(client_LoadlayoutControlsCompleted);
        //    }
        //}

        //private void LoadlayoutControls(string formid)
        //{
        //    BindId(dss.LoadlayoutControls(formid));
        //}

        //void client_LoadlayoutControlsCompleted(object sender, LoadlayoutControlsCompletedEventArgs e)
        //{
        //    BindId(e.Result.ToObservableCollection());
        //}
        //private void BindId(ObservableCollection<XElement> observableCollection)
        //{
        //    foreach (var fd in observableCollection)
        //    {
        //        tBlockFormId.Text = fd.Element("id").Value;
        //    }
        //}
    }

}